package com.hc.acoustics.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.hc.acoustics.util.YcCalculator
import com.yc.yclibrary.base.YcAppCompatActivity
import com.yc.yclibx.comment.YcEmpty
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type2_formula4_location_activity.radioButton
import kotlinx.android.synthetic.main.type2_formula4_location_activity.radioButton2
import kotlinx.android.synthetic.main.type2_formula5_location_activity.*
import kotlinx.android.synthetic.main.type2_formula7_location_activity.*
import kotlinx.android.synthetic.main.weight_text_edit.*
import kotlinx.android.synthetic.main.weight_text_edit.view.*
import java.lang.NumberFormatException

/**
 *  声学 - 房间共振频率计算
 */
class Type2Formula7Activity : BaseActivity() {

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type2Formula7Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type2_formula7_location_activity
    }

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula2_7_l_TEV.reset()
            formula2_7_w_TEV.reset()
            formula2_7_h_TEV.reset()
            formula2_7_p1.text = "-"
            formula2_7_q1.text = "-"
            formula2_7_r1.text = "-"
            formula2_7_p2.text = "-"
            formula2_7_q2.text = "-"
            formula2_7_r2.text = "-"
            formula2_7_p3.text = "-"
            formula2_7_q3.text = "-"
            formula2_7_r3.text = "-"
        }
        formulaOkBtn.setOnClickListener {
            try {
                val resultP1 = FormulaUtil.formula2_7_1(formula2_7_l_TEV.getDouble(), 1.0)
                val resultQ1 = FormulaUtil.formula2_7_1(formula2_7_w_TEV.getDouble(), 1.0)
                val resultR1 = FormulaUtil.formula2_7_1(formula2_7_h_TEV.getDouble(), 1.0)
                val resultP2 = FormulaUtil.formula2_7_1(formula2_7_l_TEV.getDouble(), 2.0)
                val resultQ2 = FormulaUtil.formula2_7_1(formula2_7_w_TEV.getDouble(), 2.0)
                val resultR2 = FormulaUtil.formula2_7_1(formula2_7_h_TEV.getDouble(), 2.0)
                val resultP3 = FormulaUtil.formula2_7_1(formula2_7_l_TEV.getDouble(), 3.0)
                val resultQ3 = FormulaUtil.formula2_7_1(formula2_7_w_TEV.getDouble(), 3.0)
                val resultR3 = FormulaUtil.formula2_7_1(formula2_7_h_TEV.getDouble(), 3.0)
                setText(formula2_7_p1, resultP1)
                setText(formula2_7_q1, resultQ1)
                setText(formula2_7_r1, resultR1)
                setText(formula2_7_p2, resultP2)
                setText(formula2_7_q2, resultQ2)
                setText(formula2_7_r2, resultR2)
                setText(formula2_7_p3, resultP3)
                setText(formula2_7_q3, resultQ3)
                setText(formula2_7_r3, resultR3)
            } catch (e: NumberFormatException) {
                formula2_7_p1.text = "Infinity"
                formula2_7_q1.text = "Infinity"
                formula2_7_r1.text = "Infinity"
                formula2_7_p2.text = "Infinity"
                formula2_7_q2.text = "Infinity"
                formula2_7_r2.text = "Infinity"
                formula2_7_p3.text = "Infinity"
                formula2_7_q3.text = "Infinity"
                formula2_7_r3.text = "Infinity"
            }
        }
    }

    fun getDouble(text: TextView): Double? {
        if (YcEmpty.isEmpty(text.text.toString())) {
            return null
        } else {
            return text.text.toString().toDouble()
        }
    }

    @SuppressLint("SetTextI18n")
    fun setText(textView: TextView, text: Double?) {
        if (text == null) {
            textView.text = "-"
        } else {
            textView.text = YcCalculator.replaceZero(YcCalculator.roundOff(text).toString()) + "Hz"
        }
    }
}